package org.wlgzs.office_exam_student.servlet;


import com.songminju.jdagger.JDEncode;
import org.wlgzs.office_exam_student.entity.User;
import org.wlgzs.office_exam_student.permission.Authentication;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author  孤城落寞博客
 *     查看个人资料
 */
@WebServlet("/myInfo")
public class MyInfoServlet extends BaseServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("application/text; charset=utf-8");

        //获取当前session 中的数据
        User user= Authentication.user();

        //从前台接受参数
        String  name = request.getParameter("name");
        String  phone = request.getParameter("phone");
        String  password = request.getParameter("password");

        logger.debug(name+" "+phone+" "+password);


        //参数判断  防注入
        if(password !=null && password.length() <= 128 && password.length()>6){
            user.setPassword(password);
        }

        if(phone.length() == 11 && name.length()<=4 && name.length()>=2){
            user.setPhone(phone);
            user.setName(name);

            //执行sql 语句
            Object updateUserNum = userService.updateUser(user);

            logger.debug("密码修改情况",updateUserNum+"");

            if((updateUserNum+"").equals("1")){
                renderString("资料修改成功");
                logger.debug("资料修改成功");
            }else{
                renderString("资料修改失败,请稍后再试!!");
                logger.debug("资料修改失败,请稍后再试!!");
            }

        }else{
            renderString("资料修改失败,请仔细检查数据后在试!!");
            logger.debug("资料修改失败,请仔细检查数据后在试!!");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取当前session 中的数据
        User user= Authentication.user();
        request.setAttribute("user",user);
        render("myInfo");
    }
}
